package com.note.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.ano.Log;
import com.common.vo.Result;
import com.note.domain.dto.PublishNoteDTO;
import com.note.domain.dto.UpdateNoteDTO;
import com.note.domain.entity.Note;
import com.note.domain.vo.NoteStatsVO;
import com.note.service.INoteService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 笔记表 前端控制器
 * </p>
 *
 * @author 坤哥
 * @since 2025-10-11
 */
@RestController
@RequestMapping("/note")
@Slf4j
public class NoteController {

    @Resource
    private INoteService noteService;

    /**
     * 发布笔记
     */
    @PostMapping("/publish")
    @Log(operationType = "新增", operationModule = "笔记管理", operationDesc = "发布笔记")
    public Result<String> publishNote(@RequestBody PublishNoteDTO publishNoteDTO) {
        log.info("发布笔记: {}", publishNoteDTO);
        noteService.publishNote(publishNoteDTO);
        return Result.ok("发布成功");
    }

    /**
     * 获取个人笔记列表（分页）
     */
    @GetMapping("/list")
    @Log(operationType = "查询", operationModule = "笔记管理", operationDesc = "查询个人笔记列表")
    public Result<IPage<Note>> getNoteList(
            @RequestParam(required = false) String title,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        log.info("查询个人笔记列表: title={}, pageNum={}, pageSize={}", title, pageNum, pageSize);
        IPage<Note> page = noteService.getNoteList(title, pageNum, pageSize);
        return Result.ok(page);
    }

    /**
     * 获取所有笔记列表（分页，管理员）
     */
    @GetMapping("/allList")
    @Log(operationType = "查询", operationModule = "笔记管理", operationDesc = "查询所有笔记列表")
    public Result<IPage<Note>> getAllNoteList(
            @RequestParam(required = false) String title,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        log.info("查询所有笔记列表: title={}, pageNum={}, pageSize={}", title, pageNum, pageSize);
        IPage<Note> page = noteService.getAllNoteList(title, pageNum, pageSize);
        return Result.ok(page);
    }

    /**
     * 删除笔记（软删除）
     */
    @DeleteMapping("/delete/{id}")
    @Log(operationType = "删除", operationModule = "笔记管理", operationDesc = "删除笔记")
    public Result<String> deleteNote(@PathVariable Long id) {
        log.info("删除笔记: id={}", id);
        noteService.deleteNote(id);
        return Result.ok("删除成功");
    }

    /**
     * 更新笔记
     */
    @PutMapping("/update")
    @Log(operationType = "修改", operationModule = "笔记管理", operationDesc = "更新笔记")
    public Result<String> updateNote(@RequestBody UpdateNoteDTO updateNoteDTO) {
        log.info("更新笔记: {}", updateNoteDTO);
        noteService.updateNote(updateNoteDTO);
        return Result.ok("更新成功");
    }

    /**
     * 获取笔记统计数据
     */
    @GetMapping("/stats")
    @Log(operationType = "查询", operationModule = "笔记管理", operationDesc = "查询笔记统计数据")
    public Result<NoteStatsVO> getNoteStats() {
        log.info("查询笔记统计数据");
        NoteStatsVO stats = noteService.getNoteStats();
        return Result.ok(stats);
    }
}

